<!-- 护理项目列表页 -->
<template>
  <div class="min-h serveProject bg-wt">
         <Seacher
         :searchData="pagination"
         @handleClear="handleClear"
         @handleSearch="handleSearch"
         @handleReset="handleReset"
          ></Seacher>
         <TableList
         @handleEdit="handleEdit"
         @handleBulid="handleBulid"
         @handleClickDelete="handleClickDelete"
         @handleForbidden="handleForbidden"
         @getCurrent="getCurrent"
         :total="total"
         :listData="listData"
         :pagination="pagination"
          ></TableList>
          <DialogForm
          :visible="visible"
          @handleClose="handleClose"
          :data="formBaseData"
          :title="title"
          @handleEditFrom="handleEditFrom"
          ref="formRef"
          @handleAdd="handleAdd" 
        ></DialogForm>
  </div>
</template>

<script setup lang="ts">
   import  TableList from './components/TableList.vue'
   import Seacher from './components/SearchList.vue'
   import  { ref, onMounted,watch } from 'vue'
 import { MessagePlugin,DialogPlugin } from 'tdesign-vue-next'
 import DialogForm from './components/DialogFrom.vue'

// 接口
import { getProjectList,projectDelete,projectStatus,projectAdd,getProjectDetails,projectUpdate} from '@/api/serve'
  
// ------定义变量------
const visible = ref(false) // 新增，编辑弹窗
const listData = ref([]) // 列表数据
const dataLoading = ref(false) // 加载中
const formBaseData = ref({}) // 弹窗表单内容
const title = ref('') // 弹窗标题
const total = ref(0) // 总条数
const projectData = ref([]) // 护理项目数据
const formRef = ref(null)

const pagination = ref({
  pageNum: 1,
  pageSize: 5,
  total: 0,
})
onMounted(() => {
  getList()
})

//修改
const handleEditFrom=async(val)=>{
     const res=await projectUpdate(val);
     if(res.code==200){
      MessagePlugin.success('修改成功');
       formRef.value.handleClear();
        getList();
       visible.value=false;
     }else{
       MessagePlugin.error(res.msg)
     }
}
//新增
const handleAdd=async(val)=>{
    const res=await projectAdd(val);
    if(res.code==200){
      MessagePlugin.success('新增成功');
       formRef.value.handleClear();
        getList();
       visible.value=false;
    }else{
      MessagePlugin.error("新增失败")
    }
}

const handleBulid=()=>{
   visible.value=true;
}

const handleClose=()=>{
   visible.value=false;
}
const handleEdit=async(row)=>{
  if(row.count==0){
   visible.value=true;
   title.value="编辑";
   const res=await getProjectDetails(row.id);
   if(res.code==200){
      formBaseData.value=res.data;
    }else{
      MessagePlugin.error(res.msg)
    }
  }
  }

const  getCurrent = async(val) => {
   pagination.value.pageNum = val.current
   pagination.value.pageSize = val.pageSize
   getList()
}
//搜索
const handleSearch=async()=>{
  pagination.value.pageNum = 1
    getList();
}
const handleReset=async()=>{
   getList();
}
const handleClear=async(val)=>{
    if(val=="status"){
      pagination.value.status=""
    }else{
      pagination.value.name=""
    }
    getList();
}
const handleClickDelete = async (row) => {
  if(row.count==0){
  const confirmDialog = DialogPlugin.confirm({
    header: '确定要删除吗',
    body: '是否确认删除？',
    confirmBtn: {
      content: '确定',
      theme: 'primary',
      loading: false,
    },
    theme: 'warning',
    onConfirm: async () => {
      try {
        // 更新按钮状态为加载中
        confirmDialog.update({
          confirmBtn: {
            content: '删除中',
            loading: true,
          },
        });

        // 模拟删除操作
        console.log(row.id);
        const res = await projectDelete(row.id);
             console.log(res);    
        // 检查删除结果
        if (res.code === 200) {
          MessagePlugin.success('删除成功');
          getList();
        } else {
          MessagePlugin.error('删除失败');
        }
      } catch (error) {
        console.error('删除过程中发生错误:', error);
        MessagePlugin.error('删除失败');
      } finally {
        // 无论成功或失败，都隐藏对话框并重置按钮状态
        confirmDialog.update({
          confirmBtn: {
            content: '确定',
            loading: false,
          },
        });
        confirmDialog.hide();
      }
    },
  });
  }
};
const disable=ref("禁用");
const handleForbidden=async(row)=>{
  console.log(row.count+"=================================");
if(row.count==0){
  if(row.status==0){
     disable.value="启用"
   }else{
     disable.value="禁用"
   }
  const confirmDialog = DialogPlugin.confirm({
    header: "确定要"+disable.value+"吗",
    body: '是否确认'+disable.value+"？",
    confirmBtn: {
      content: '确定',
      theme: 'primary',
      loading: false,
    },
    theme: 'warning',
    onConfirm: async () => {
      try {
        // 更新按钮状态为加载中
        confirmDialog.update({
          confirmBtn: {
            content:  disable.value+'中',
            loading: true,
          },
        });
        const res=await projectStatus(row)
  if(row.status==1){
    if(res.code==200){
       MessagePlugin.success('禁用成功'); 
         getList()
      }else{
     MessagePlugin.error('操作失败');
   }
    }else{  
        if(res.code==200){
           getList()
          MessagePlugin.success('启用成功'); 
      }else{
     MessagePlugin.error('操作失败');
     }
     }   
      } catch (error) {
        console.error('删除过程中发生错误:', error);
        MessagePlugin.error(disable.value+'失败');
      } finally {
        // 无论成功或失败，都隐藏对话框并重置按钮状态
        confirmDialog.update({
          confirmBtn: {
            content: '确定',
            loading: false,
          },
        });
        confirmDialog.hide();
      }
    },
  });
  }
}
// ------定义方法------
// 获取列表数据
const getList = async () =>{
  try {
    const res = await getProjectList(pagination.value);
    listData.value = res.data.records;
    console.log(listData.value);   
    total.value = Number(res.data.total)
    console.log(total.value);   
  } catch (error) {
    console.log(error)
  }
}
// 搜索





</script>
